package spider

import (
	"encoding/json"
	"strconv"
)

type ResponseSchool struct {
	Code    any     `json:"code"`
	Message any     `json:"message"`
	Data    DataKey `json:"data"`
}

type DataKey struct {
	Data map[string]ProvinceData `json:"data"`
}
type ProvinceData map[string]YearData

type YearData struct {
	Zero  DigitData `json:"0"`
	One   DigitData `json:"1"`
	Level LevelData `json:"level"`
}

type LevelData struct {
	Zero DigitData `json:"0"`
	One  DigitData `json:"1"`
}

type DigitData struct {
	ID   any `json:"id"`
	Name any `json:"name"`
}

func (d DigitData) IDToStr() string {
	s, ok := d.ID.(string)
	if ok {
		return s
	}
	i, ok := d.ID.(int)
	if ok {
		return strconv.Itoa(i)
	}
	f, ok := d.ID.(float64)
	if ok {
		return strconv.Itoa(int(f))
	}
	return ""
}
func (d DigitData) NameToStr() string {
	s, ok := d.Name.(string)
	if ok {
		return s
	}
	i, ok := d.Name.(int)
	if ok {
		return strconv.Itoa(i)
	}
	f, ok := d.Name.(float64)
	if ok {
		return strconv.Itoa(int(f))
	}
	return ""
}

func UnmarshalDivideSectionJsonStr(jsonStr []byte) (res ResponseSchool) {
	err := json.Unmarshal(jsonStr, &res)
	if err != nil {
		panic("解析json失败:" + err.Error())
	}
	return
}
